package com.budwk.app.sms.service.impl;

import com.budwk.app.sms.constants.SysSmsConstant;
import com.budwk.app.sms.dto.SmsMsgDTO;
import com.budwk.app.sms.service.CommonSmsService;
import com.budwk.app.sms.service.OpaSmsService;
import com.budwk.app.sms.service.TencentSmsService;
import com.budwk.app.sqfw.models.Sqfw_servicerecord;
import com.budwk.app.sqfw.models.Sqfw_workorder;
import com.budwk.app.sys.models.Sys_config;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysConfigService;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZY.Wang
 * @since 2023/6/28
 */
@IocBean
public class CommonSmsServiceImpl implements CommonSmsService {

    @Inject
    private SysConfigService sysConfigService;
    @Inject
    private OpaSmsService opaSmsService;
    @Inject
    private TencentSmsService tencentSmsService;

    @Override
    public void sendSms(Sqfw_servicerecord serviceRecord,Sqfw_workorder workOrder,Sys_user sendSysUser,Sys_user receiveSysUser) {

        String smsChannel = "";
        List<Sys_config> sysConfigList = sysConfigService.query(Cnd.where("appId", "=", "COMMON").and("configkey", "=", "smsChannel"));
        if (sysConfigList.size() > 0) smsChannel = sysConfigList.get(0).getConfigValue();
        else smsChannel = SysSmsConstant.poaSms;

        if (smsChannel.equals(SysSmsConstant.poaSms)) {
            // 使用opa发送短信
            SmsMsgDTO msgDTO = createPoaSmsMsgDTOV2(serviceRecord, workOrder, sendSysUser, receiveSysUser);
            opaSmsService.opaSmsSendOneMessage(msgDTO);
        } else {
            // 使用腾讯sms发送
            SmsMsgDTO msgDTO = new SmsMsgDTO();
            msgDTO.setPhoneNumberSet(Sets.newHashSet(receiveSysUser.getLoginname()));
            msgDTO.setTemplateParamList(Lists.newArrayList(sendSysUser.getUsername(), "金元"));
            tencentSmsService.txSmsSendOneMessage(msgDTO);
        }
    }


    private SmsMsgDTO createPoaSmsMsgDTOV3(Sqfw_servicerecord serviceRecord, Sqfw_workorder workOrder, Sys_user senderSysUser, Sys_user receiveSysUser) {
        SmsMsgDTO msgDTO = new SmsMsgDTO();
        msgDTO.setPhoneNumberSet(Sets.newHashSet(receiveSysUser.getLoginname()));

        String type = serviceRecord.getType();

        String priorityPrefix = "优先级";
        String priority = "";
        String wkPriority = workOrder.getWk_priority();
        if (StringUtils.isBlank(wkPriority)) priority = "take_time_complete";
        if (wkPriority.equals("complete_immediately")) priority = "高";
        if (wkPriority.equals("prioritize_completion")) priority = "中";
        if (wkPriority.equals("take_time_complete")) priority = "低";
        if (StringUtils.isBlank(wkPriority)) priority = "低";
        priority = priorityPrefix + priority;

        String remark = serviceRecord.getRemarks();
        if (StringUtils.isBlank(remark)) remark = "无";
        if (remark.length() > 20) remark = remark.substring(0, 20) + "...";

        ArrayList<String> paramList = Lists.newArrayList(senderSysUser.getUsername(), type, priority, remark);
        msgDTO.setTemplateParamList(paramList);

        return msgDTO;
    }

    private SmsMsgDTO createPoaSmsMsgDTOV2(Sqfw_servicerecord serviceRecord, Sqfw_workorder workOrder, Sys_user senderSysUser, Sys_user receiveSysUser) {
        SmsMsgDTO msgDTO = new SmsMsgDTO();
        msgDTO.setPhoneNumberSet(Sets.newHashSet(receiveSysUser.getLoginname()));

        String type = serviceRecord.getType();

        String priorityPrefix = "优先级";
        String priority = "";
        String wkPriority = workOrder.getWk_priority();
        if (StringUtils.isBlank(wkPriority)) priority = "take_time_complete";
        if (wkPriority.equals("complete_immediately")) priority = "高";
        if (wkPriority.equals("prioritize_completion")) priority = "中";
        if (wkPriority.equals("take_time_complete")) priority = "低";
        if (StringUtils.isBlank(wkPriority)) priority = "低";
        priority = priority + priorityPrefix;

        String remark = serviceRecord.getCustomer_need();
        if (StringUtils.isBlank(remark)) remark = "无";
        if (remark.length() > 20) remark = remark.substring(0, 20) + "...";

        String contentParam = senderSysUser.getUsername() + "发给您一条" + type + "工作流，" + priority + "，备注：" + remark;

        ArrayList<String> paramList = Lists.newArrayList(contentParam);
        msgDTO.setTemplateParamList(paramList);

        msgDTO.setTempId("861352845932765184");

        return msgDTO;
    }
}
